package controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import service.ClassroomTableService;
import utils.MultipartFileDto;
import utils.PageObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author W M J
 * @ClassName: Test
 * @Description: TODO(教室设置对应controller层)
 * @date 2021/12/27 10:53
 */
@RequestMapping("/classroom")
@Controller
public class classroomController {
    @Autowired
    private ClassroomTableService classroomTableService;
    @RequestMapping("/selectall")
    @CrossOrigin
    @ResponseBody
    public PageObject classselect(@RequestBody JSONObject object){
        System.out.println(object.toString());
        PageObject pageObject = classroomTableService.queryAllByLimit(object);
        return pageObject;
    }

    @RequestMapping("/classroominsert")
    @ResponseBody
    @CrossOrigin
    public  JSONObject insertone(@RequestBody JSONObject insertobj){
        return classroomTableService.insert(insertobj);
    }

    @RequestMapping("/delete")
    @ResponseBody
    @CrossOrigin
    public boolean delete(@RequestBody JSONObject insertobj){
        try {
            return   classroomTableService.deleteById(insertobj.getInteger("pkId"));
        }catch (Exception E){
            return false;
        }


    }

    @RequestMapping("/excelimport")
    @ResponseBody
    @CrossOrigin
    public JSONObject excelimport(@RequestBody JSONObject fileobj) throws Exception {
        System.out.println("========="+fileobj.toString());
        String file = fileobj.getString("file");
        System.out.println(file);
        File excelfile=new File(file);
        FileInputStream fileInputStream=new FileInputStream(file);
        MultipartFile multipartFile=new MultipartFileDto(excelfile.getName(),excelfile.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(),fileInputStream);
        JSONObject excelimport = classroomTableService.excelimport(multipartFile);
        return excelimport;
    }


    @RequestMapping("/saveExcel")
    @ResponseBody
    @CrossOrigin
    public Object saveExcel(@RequestBody JSONObject fileobj) throws Exception {
        String file = fileobj.getString("fileobj");
        System.out.println(file);
        return 0;
    }
}
